//
//  LGTMessageListVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/9/3.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import ZHRefresh

class LGTMessageListVC: LGTBaseVC, UITableViewDataSource, UITableViewDelegate{
    
    var tableView:UITableView?
    var page:Int = 1
    var type:Int = 0 //0 消息中心  1系统公告
    let Notice_List_URL:String = API_URL("notice/index")
    var data:Array<Dictionary<String, Any>> = []
        override func viewDidLoad() {
        super.viewDidLoad()
        self.setBlock()
    }
    
    func setBlock(){
        weak var weakSelf = self
        self.reloadBlock = {() in
            weakSelf?.tableView?.header?.beginRefreshing()
        }
    }
    
    
    override func setUI() {
        super.setUI()
       // self.setBavBarStyle(navBarHiden: true, title: "消息通知", back: false)
        
        let w = Double(self.view.frame.width)
        let h = Double(self.view.frame.height) - NavBarH() - SizeFit(44)
        self.tableView = UITableView.init(frame:  CGRect.init(x: 0, y: 0, width: w, height: h), style: UITableView.Style.plain)
        
        self.view.addSubview(self.tableView!)
        self.tableView?.backgroundColor = ViewBackColor
        self.tableView?.separatorStyle = UITableViewCell.SeparatorStyle.none
        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        self.tableView?.estimatedRowHeight = 50
        self.tableView?.rowHeight = UITableView.automaticDimension
        self.tableView?.register(LGTMessageCell.classForCoder(), forCellReuseIdentifier: "cell")
        
        weak var weakSelf = self
        let header = ZHRefreshNormalHeader.headerWithRefreshing(block: {
        weakSelf?.page = 1
        weakSelf?.getData()
        })
        self.tableView?.header = header
    
    
        self.tableView?.footer = ZHRefreshBackNormalFooter.footerWithRefreshing(block: {
            weakSelf?.page += 1
            weakSelf?.getData()
            })
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:LGTMessageCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LGTMessageCell
        cell.model = self.data[indexPath.row]
        return cell
    }
    
    func getData(){
        weak var weakSelf = self
        var url:String = Message_List_URL
        if self.type == 1{
            url = Notice_List_URL
        }
        let param:Dictionary<String, Any> = ["page": weakSelf!.page]
        LGTHttpUtil.httpUtil.requestData(url: url, param: param, showMessage: false, success: { (json) in
            weakSelf?.tableView?.header?.endRefreshing()
            weakSelf?.tableView?.footer?.endRefreshing()
            let dataArray:Array<Dictionary<String, Any>> = json["data"] as? Array<Dictionary<String, Any>> ?? []
            if weakSelf?.page == 1{
                weakSelf?.data = dataArray
            }else{
                weakSelf?.data.append(contentsOf: dataArray)
            }
            if dataArray.count == 0{
                weakSelf?.tableView?.footer?.endRefreshingWithNoMoreData()
            }else{
                weakSelf?.tableView?.footer?.resetNoMoreData()
            }
            if weakSelf?.data.count == 0{
                weakSelf?.showNoData(image: NoData_Image, text: NoData_Text, subText: "", haveNav:false)
            }
            
            weakSelf?.tableView?.reloadData()
        }, failure: { (json) in
            weakSelf?.tableView?.header?.endRefreshing()
            weakSelf?.tableView?.footer?.endRefreshing()
        })
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let dict:Dictionary<String, Any> = self.data[indexPath.row]
        let url:String = dict["url"] as? String ?? ""
        LGTViewUtil.share.gotoVC(nav: self.navigationController!, currentVC: self, urlStr:url)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if self.data.count == 0{
            self.tableView?.header?.beginRefreshing()
        }
    }
}
